package com.cn.wanxi.control;

import com.cn.wanxi.common.Message;
import com.cn.wanxi.entity.ExamMaintainEntity;
import com.cn.wanxi.service.ExamMaintainService;
import net.sf.json.JSONArray;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @Author:LLH
 * @Date:2020/3/16 9:40
 * @Description:  试题维护控制层
 */

@Controller
@RequestMapping("/exam")
public class ExamMaintainControl {
    @Autowired
    ExamMaintainService examMaintainService;
    @Autowired
    private HttpServletRequest request;

    @RequestMapping("/findAll")
    @ResponseBody
    public void findAll(HttpServletResponse response) throws IOException {
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
        List<ExamMaintainEntity> findAll = examMaintainService.findAll();
        JSONArray jsonArray = JSONArray.fromObject(new Message(1,"",findAll));
        response.getWriter().println(jsonArray.toString());
    }

    @RequestMapping("/finaById")
    @ResponseBody
    public void finaById(HttpServletResponse response) throws IOException {
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
        String question_id = request.getParameter("question_id");
        int id = question_id != null && !question_id.equals("") ? Integer.parseInt(question_id) : 0;
        JSONArray jsonArray = JSONArray.fromObject(new Message(1,"",examMaintainService.findById(id)));
        response.getWriter().println(jsonArray.toString());
    }

    @RequestMapping("/add")
    @ResponseBody
    public void add(HttpServletResponse response,ExamMaintainEntity examMaintainEntity) throws IOException {
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
        int a = examMaintainService.add(examMaintainEntity);
        JSONArray jsonArray = JSONArray.fromObject(new Message(1,a == 1 ? "新增成功" : "新增失败",a));
        response.getWriter().println(jsonArray.toString());
    }

    @RequestMapping("/delete")
    @ResponseBody
    public void delete(HttpServletResponse response) throws IOException {
        String question_id = request.getParameter("question_id");
        int id = question_id != null && !question_id.equals("") ? Integer.parseInt(question_id) : 0;
        JSONArray jsonArray = JSONArray.fromObject(new Message(1,"",examMaintainService.del(id)));
        response.getWriter().println(jsonArray.toString());
    }

    @RequestMapping("/update")
    @ResponseBody
    public void update(HttpServletResponse response,ExamMaintainEntity examMaintainEntity) throws IOException {
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
        int result = examMaintainService.update(examMaintainEntity);
        JSONArray jsonArray = JSONArray.fromObject(new Message(1,"",result));
        response.getWriter().println(jsonArray.toString());
    }

    @RequestMapping("/findAllLevel")//难易等级
    @ResponseBody
    public void findAllLevel(HttpServletResponse response) throws IOException {
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
        examMaintainService.findAllLevel();
        JSONArray jsonArray = JSONArray.fromObject(new Message(1,"", examMaintainService.findAllLevel()));
        response.getWriter().println(jsonArray.toString());
    }

    @RequestMapping("/findAllType")//试题类型
    @ResponseBody
    public void findAllType(HttpServletResponse response) throws IOException {
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
        examMaintainService.findAllType();
        JSONArray jsonArray = JSONArray.fromObject(new Message(1,"", examMaintainService.findAllType()));
        response.getWriter().println(jsonArray.toString());
    }

    @RequestMapping("/findAllDirection")//试题方向
    @ResponseBody
    public void findAllDirection(HttpServletResponse response) throws IOException{
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
        examMaintainService.findAllDirection();
        JSONArray jsonArray = JSONArray.fromObject(new Message(1,"", examMaintainService.findAllDirection()));
        response.getWriter().println(jsonArray.toString());
    }
}
